from Tools import EliminatingBytePadding
from Tools import EncodeMainTextByAESCBC, DecodeAllTextByAESCBC, EncodeMainKeyByAESCBC, EncodeAllTextByAESCBC
from Sse import DecodeBySSE, SSEForKey
import requests
from hmacs import HmacBack
import base64

class user():

    #用户id
    id = 0

    #text加密参数
    key = b"1234567891234567"

    IV = b"1234567891234567"

    #text加密参数
    keys = b"1234565785134567"

    IVS = b"1234565785134567"

    #随机数seed
    seed  = [95175346, 57384910]

    #hmac初始key
    hashkey = b"5678456846869"

    #传输加密参数
    transkey = b"1245558767851456"

    transIV = b"1245558767851456"

    url = "http://127.0.0.1:5000"
    
    def __init__(self, id):
        self.id = self.id + id
    
    def GetID(self):
        return self.id

    def SaveText(self, text, keyword):
        #AES
        #摘要
        ciper = EncodeMainTextByAESCBC(text, self.key, self.IV)
        
        #关键词
        ciperkey = EncodeMainKeyByAESCBC(keyword, self.keys, self.IVS)
        
        #SSE
        #关键词
        ciperkey = SSEForKey(ciperkey, self.hashkey, self.seed)
        
        return ciper, ciperkey

    def Storge(self, text, key):
        
        #传输前加密
        text = EncodeAllTextByAESCBC(text, self.transkey, self.transIV)
        key = EncodeAllTextByAESCBC(key, self.transkey, self.transIV)
        
        #用base64加密，以便传输
        text = base64.b64encode(text)
        text = str(text, encoding="utf-8")

        key = base64.b64encode(key)
        key = str(key, encoding="utf-8")
        
        #参数，json格式
        save = {
            "id" : self.id,
            "text" : text,
            "key" : key
        }
        
        #url 
        urls = self.url + "/test"

        #存储
        reponse  = requests.post(urls, params=save)
        
        reponse.close()
        
    
    def Find(self, key):
        #AESCBC
        #零填充
        l = 48-3*len(key)+2
        while(l != 0):
            key += "0"
            l -= 1
        
        #加密
        key = bytes(key, encoding="utf8") 
        key = EncodeAllTextByAESCBC(key, self.keys, self.IVS)

        #随机数
        s = key[:16]

        #传输前加密
        key = EncodeAllTextByAESCBC(key, self.transkey, self.transIV)
        key = base64.b64encode(key) 
        key = str(key, encoding="utf-8")

        #k
        k = HmacBack(s, self.hashkey)
        k = EncodeAllTextByAESCBC(k, self.transkey, self.transIV)
        k = base64.b64encode(k) 
        k = str(k, encoding="utf-8")


        #参数,json
        find = {
            "id" : self.id,
            "k" : k,
            "key" : key
        }

        #url 
        urls = self.url + "/find"

        #查询
        reponse  = requests.post(urls, params=find)

        reponse.close()

        back = reponse.json()
        back = back["ans"]
        ans = []
        if(back == "www"):
            ans.append(["好像没有wwww", "换个关键词吧"])
        else:
            for find in back:
                text = find["text"]
                text = bytes(text, encoding="utf-8")
                text = base64.b64decode(text) 
                text = DecodeAllTextByAESCBC(text, self.transkey, self.transIV)
                text = DecodeAllTextByAESCBC(text,self.key, self.IV)
                text = EliminatingBytePadding(text)
                text = text.decode()
                

                key = find["key"]
                key = bytes(key, encoding="utf-8")
                key = base64.b64decode(key) 
                key = DecodeAllTextByAESCBC(key, self.transkey, self.transIV)
                key = DecodeBySSE(key, self.hashkey, self.seed)
                key = DecodeAllTextByAESCBC(key, self.keys, self.IVS)
                key = key.decode()
                key = key.replace('0','')
                
                ans.append([text, key])
            
        return ans

   
     
